﻿@page "/identity/users/{Id}/profile"


<PageHeader Title="@Title" Header="@Title" SubHeader="@Description" />
@if (!_loaded)
{
    <MudProgressCircular Color="Color.Primary" Indeterminate="true" />
}
else
{
    <MudGrid>
        <AuthorizeView>
            @if (_canToggleUserStatus)
            {
                <MudItem xs="12" sm="12" md="12">
                    <MudCard Outlined>
                        <MudCardHeader>
                            <CardHeaderContent>
                                <MudText Typo="Typo.body1">Administrator Settings.</MudText>
                                <MudText Typo="Typo.body2">This is an Administrator Only View.</MudText>
                            </CardHeaderContent>
                        </MudCardHeader>
                        <MudCardContent Class="pa-46" Style="padding:26px!important">
                            <MudGrid Style="padding-left:14px">
                                    <MudSwitch T="bool" @bind-Value="@_active" Color="Color.Primary" Label="Active?"></MudSwitch>
                                <MudSwitch T="bool" ReadOnly @bind-Value="@_emailConfirmed" Disabled Label="Email Confirmed?" Color="Color.Secondary"></MudSwitch>
                                <MudButton Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto"
                            OnClick="ToggleUserStatus" ButtonType="ButtonType.Submit">Save Changes
                                </MudButton>
                            </MudGrid>
                        </MudCardContent>
                    </MudCard>
                </MudItem>
            }
        </AuthorizeView>
        <MudItem xs="12" sm="4" md="4">
            <MudCard Outlined>
                <MudCardContent Style="padding:0px!important">
                    <div class="d-flex justify-center mb-4">
                        @if (_imageUrl != null)
                        {
                            <MudAvatar Style=" width:100%;height:auto;border-radius:0px!important;">
                                <MudImage Src="@_imageUrl.ToString()"></MudImage>
                            </MudAvatar>
                        }
                        else
                        {
                            <MudAvatar Square="true" Color="Color.Primary" Style="width:100%;height:300px;">@_firstLetterOfName
                            </MudAvatar>
                        }
                    </div>
                    <MudText Typo="Typo.h6" Align="Align.Center">@_firstName @_lastName</MudText>
                    <MudText Align="Align.Center">@_email</MudText>
                </MudCardContent>
                <MudCardActions Class="d-flex justify-center">
                    <div style="padding-bottom:20px">
                        @if (_imageUrl != null)
                        {
                            <MudButton HtmlTag="label" DropShadow="false" Variant="Variant.Filled" Color="Color.Info"
                                       Size="Size.Small" Target="_blank" StartIcon="@Icons.Material.Filled.RemoveRedEye" Href="@_imageUrl.ToString()"
                        Style="margin-left: 5px;">
                               View
                            </MudButton>
                        }
                    </div>

                </MudCardActions>
            </MudCard>
        </MudItem>
        <MudItem xs="12" sm="8" md="8">
            <MudCard Outlined>
                <MudCardHeader>
                    <CardHeaderContent>
                        <MudText>Public Profile</MudText>
                    </CardHeaderContent>
                </MudCardHeader>
                <MudCardContent>
                    <MudGrid>
                        <MudItem xs="12" md="6">
                            <MudField Label="First Name" Variant="Variant.Outlined">@_firstName</MudField>
                        </MudItem>
                        <MudItem xs="12" md="6">
                            <MudField Label="Last Name" Variant="Variant.Outlined">@_lastName</MudField>
                        </MudItem>
                        <MudItem xs="12" md="6">
                            <MudField Label="Phone Number" Variant="Variant.Outlined">@_phoneNumber
                            </MudField>
                        </MudItem>
                        <MudItem xs="12" md="6">
                            <MudField Label="Email" Variant="Variant.Outlined">@_email</MudField>
                        </MudItem>
                    </MudGrid>
                </MudCardContent>
            </MudCard>
        </MudItem>
    </MudGrid>
}

@code
{
public class CustomStringToBoolConverter : BoolConverter<string>
    {

        public CustomStringToBoolConverter()
        {
            SetFunc = OnSet;
            GetFunc = OnGet;
        }
        private string TrueString = "User Active";
        private string FalseString = "no, at all";
        private string NullString = "I don't know";

        private string OnGet(bool? value)
        {
            try
            {
                return (value == true) ? TrueString : FalseString;
            }
            catch (Exception e)
            {
                UpdateGetError("Conversion error: " + e.Message);
                return NullString;
            }
        }

        private bool? OnSet(string arg)
        {
            if (arg == null)
                return null;
            try
            {
                if (arg == TrueString)
                    return true;
                if (arg == FalseString)
                    return false;
                else
                    return null;
            }
            catch (FormatException e)
            {
                UpdateSetError("Conversion error: " + e.Message);
                return null;
            }
        }

    }
}